package sqldb

import (
	"errors"
	"fmt"
	"time"

	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

var db *gorm.DB
var err error

type Videos struct {
	Videoname string
	Tag1      string
	Tag2      string
	Tag3      string
	Uptime    time.Time
}

func init() {
	db, err = gorm.Open(mysql.New(mysql.Config{
		DSN:                       "usergo:123456@tcp(127.0.0.1:3306)/qiniucloud?charset=utf8&parseTime=True&loc=Local", // data source name
		DefaultStringSize:         256,                                                                                  // default size for string fields
		DisableDatetimePrecision:  true,                                                                                 // disable datetime precision, which not supported before MySQL 5.6
		DontSupportRenameIndex:    true,                                                                                 // drop & create when rename index, rename index not supported before MySQL 5.7, MariaDB
		DontSupportRenameColumn:   true,                                                                                 // `change` when rename column, rename column not supported before MySQL 8, MariaDB
		SkipInitializeWithVersion: false,                                                                                // auto configure based on currently MySQL version
	}), &gorm.Config{})
	if err != nil {
		panic("failed to connect database")
	}
	fmt.Printf("db: %v\n", db)
	sqlDB, err := db.DB()
	if err != nil {
		panic("sqlDb error")
	}

	// SetMaxIdleConns sets the maximum number of connections in the idle connection pool.
	sqlDB.SetMaxIdleConns(10)

	// SetMaxOpenConns sets the maximum number of open connections to the database.
	sqlDB.SetMaxOpenConns(100)

	// SetConnMaxLifetime sets the maximum amount of time a connection may be reused.
	sqlDB.SetConnMaxLifetime(time.Hour)
}
func GetVistors() {
	// var vistor []vistors
	// result := db.Debug().Table("vistor_table").Select("vistor_id", "vistor_name", "createdate").Find(&vistor)
	// for _, vistorN := range vistor {
	// 	fmt.Println(vistorN.Vistor_id, vistorN.Vistor_name, vistorN.Createdate)
	// }
	// fmt.Println(result.RowsAffected) // 返回找到的记录数
	// fmt.Println(result.Error)        // returns error or nil
	// // 检查 ErrRecordNotFound 错误
	// errors.Is(result.Error, gorm.ErrRecordNotFound)
}
func GetNew() []string {
	res := []string{}
	var videos []Videos
	result := db.Debug().Table("videos").Select("videoname", "tag1", "tag2", "tag3", "uptime").Order("uptime").Find(&videos)
	for _, videosN := range videos {
		fmt.Println(videosN.Videoname, videosN.Tag1, videosN.Tag2, videosN.Tag3, videosN.Uptime)
		res = append(res, videosN.Videoname)
	}
	fmt.Println(result.RowsAffected) // 返回找到的记录数
	fmt.Println(result.Error)        // returns error or nil
	// 检查 ErrRecordNotFound 错误
	errors.Is(result.Error, gorm.ErrRecordNotFound)

	return res
}
func GetVideoByKinds(kinds string) []string {
	res := []string{}
	var videos []Videos
	result := db.Debug().Table("videos").Select("videoname", "tag1", "tag2", "tag3", "uptime").Where("tag1 = ?", kinds).Or("tag2 = ?", kinds).Or("tag3 = ?", kinds).Order("uptime").Find(&videos)
	for _, videosN := range videos {
		fmt.Println(videosN.Videoname, videosN.Tag1, videosN.Tag2, videosN.Tag3, videosN.Uptime)
		res = append(res, videosN.Videoname)
	}
	fmt.Println(result.RowsAffected) // 返回找到的记录数
	fmt.Println(result.Error)        // returns error or nil
	// 检查 ErrRecordNotFound 错误
	errors.Is(result.Error, gorm.ErrRecordNotFound)

	return res
}
